/**
 * Copyright (c) 2006-2007 Jinyi.lu
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of GNU LGPL license by gun.org
 * which accompanies this distribution, and is available at
 * http://gnu.feld-it.at/copyleft/lesser.html
 * 
 * Project Homepage : http://code.google.com/p/demou
 * Source Code: svn checkout http://demou.googlecode.com/svn/trunk/ demou
 * Mailing list : jinyi.lu@gmail.com
 *                stonelu@126.com
 *
 *  
 * Contributors:
 *     JinYi.Lu - initial API and implementation
 */
package cn.tworen.demou.remoting;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Http message.
 * 
 * @author <a href="chenrui@gmail.com">Roger Chen</a>
 * @version $id$
 */
public abstract class HttpMessage {

    private static final byte[] EMPTY_CONTENT = new byte[0];

    private final Map params = new LinkedHashMap();
    private String version = "HTTP/1.0";
    private byte[] content = EMPTY_CONTENT;

    public final String getVersion() {
        return version;
    }

    public final void setVersion(String version) {
        this.version = version;
    }

    public final void setParam(String key, String value) {
        if (key != null) {
            if (value == null)
                params.remove(key);
            else
                params.put(key, value);
        }
    }

    public final String getParam(String key) {
        return key == null ? null : (String) params.get(key);
    }

    public final Map getParams() {
        return Collections.unmodifiableMap(params);
    }

    public final byte[] getContent() {
        return content;
    }

    public final void setContent(byte[] content) {
        if (content == null)
            content = EMPTY_CONTENT;
        this.content = content;
    }

    public String toString() {
        StringBuffer buffer = new StringBuffer();
        for (Iterator iter = params.entrySet().iterator(); iter.hasNext();) {
            Entry entry = (Entry) iter.next();
            buffer.append(entry.getKey()).append(": ").append(entry.getValue())
                    .append("\r\n");
        }
        buffer.append("\r\n");
        return buffer.toString();
    }
}
